/**
 * <p>Title: Data Capture System</p>
 * <p>Description: 外汇交易系统数据捕获接收系统</p>
 * <p>Copyright: Copyright (c) 2005 Intenation Geong</p>
 * <p>Company: Geong</p>
 */
package com.sxds.wn.util;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

import com.sxds.wn.model.TreeNode;

/**
 * Description: <p>解析文件XML文件</p>
 * Content Desc:<p>解析文件XML文件<p>
 * Copy Right of Geong
 * @author Tangtao Email:<a href="mailto:tangtao7481@yahoo.com.cn">tangtao7481@yahoo.com.cn</a>
 * @version 1.0 Create Date:@2010-4-28
 */
public class XmlUtil {

	/**
	 * 解析XML文件产生相应的Java Bean
	 * @param file 
	 * @return java.util.List<TreeNode>
	 */
	public static List parseXmlToTreeNode(Resource file) throws Exception{
		return parseXmlToTreeNode(file.getFile().getAbsolutePath());
	}
	
	/**
	 * 解析XML文件产生相应的Java Bean
	 * @param file 
	 * @return java.util.List<TreeNode>
	 */
	public static List parseXmlToTreeNode(String file) throws Exception{
		List list = new ArrayList();
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		List elements = doc.getRootElement().elements();
		for (int i = 0; i < elements.size(); i++) {
			Element node = (Element) elements.get(i);
			TreeNode treeNode = new TreeNode();
			treeNode.setId(node.attribute("id").getValue());
			treeNode.setLeaf(Boolean.parseBoolean(node.attribute("leaf").getValue()));
			treeNode.setLink(node.attribute("link").getValue());
			treeNode.setText(node.attribute("name").getValue());
			list.add(treeNode);
		}
		return list;
	}

	
}
